package com.dididoctor.doctor.Activity.Usercentre.MyPurse.CollectionMethods;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.StrericWheelAdapter;
import com.dididoctor.doctor.Ui.WheelView;
import com.dididoctor.doctor.Utils.Util;

import java.util.List;

/**
 * Created by boy on 2016/4/22.
 */
public class AddCollectionMethodsActivity extends EduActivity implements AddCollectionMethodsView {
    private TextView mTvTitle, mTvFinish;
    private ImageView mBtnBack;

    private TextView mTvName, mTvSelectBank;
    private EditText mEtCardcode;

    private Button mBtnConfirm;

    private WheelView mWeelView;
    private Button mBtnWheelCancel, mBtnWheelConfirm;
    private StrericWheelAdapter mWheelAdapter;
    public int which = 0;
    private RelativeLayout mRelWheel;
    private AddCollcetionMethodsPresenter mPresenter;
    private List<BankBean> datas = null;
    private String cardNum = "";
    private String bankName = "";


    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_addcollectionmethods);
    }

    @Override
    public void init() {
        /**头部**/
        initTitleView();

        initView();
    }

    private void initView() {
        mTvName = (TextView) findViewById(R.id.tv_name);
        mTvName.setText(Token.getName());
        mTvSelectBank = (TextView) findViewById(R.id.tv_select_bank);
        mTvSelectBank.setOnClickListener(this);
        mEtCardcode = (EditText) findViewById(R.id.et_cardcode);

        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
        mBtnConfirm.setOnClickListener(this);

        mRelWheel = (RelativeLayout) findViewById(R.id.rel_wheel);
        mWeelView = (WheelView) findViewById(R.id.weelview);
        int textSize = dip2px(AddCollectionMethodsActivity.this, 24);
        mWeelView.TEXT_SIZE = textSize;
        mBtnWheelCancel = (Button) findViewById(R.id.btn_wheel_cancel);
        mBtnWheelCancel.setOnClickListener(this);
        mBtnWheelConfirm = (Button) findViewById(R.id.btn_wheel_confirm);
        mBtnWheelConfirm.setOnClickListener(this);

        mPresenter = new AddCollcetionMethodsPresenter(AddCollectionMethodsActivity.this, AddCollectionMethodsActivity.this);
        mPresenter.lunch();
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.tv_select_bank:
                if(datas!=null) {
                    which = 0;
                    mRelWheel.setVisibility(View.VISIBLE);
                }
                    break;
            case R.id.btn_confirm:
                if(checkValue()) {
                    mPresenter.addCard(cardNum,datas.get(mWeelView.getCurrentItem()).getBankId());
                }
                break;

            case R.id.btn_wheel_confirm:
                mTvSelectBank.setText(datas.get(mWeelView.getCurrentItem()).getBankName());
                mRelWheel.setVisibility(View.GONE);
                break;
            case R.id.btn_wheel_cancel:
                mRelWheel.setVisibility(View.GONE);
                break;
        }

    }

    private void initTitleView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("添加收款方式");
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mTvFinish.setVisibility(View.GONE);
        mTvFinish.setText("编辑");
        mBtnBack = (ImageView) findViewById(R.id.btn_back);
    }

    @Override
    public void succed(List<BankBean> datas) {
        this.datas = datas;
        String[] banks = new String[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            banks[i] = datas.get(i).getBankName();
        }
        mWheelAdapter = new StrericWheelAdapter(banks);
        mWeelView.setAdapter(mWheelAdapter);
    }

    @Override
    public void fail() {
        showToastMessage("获取银行信息失败");
    }

    @Override
    public void addSucessed() {
        setResult(RESULT_OK);
        AddCollectionMethodsActivity.this.finish();
    }

    public boolean checkValue(){
            cardNum = mEtCardcode.getText().toString().trim();
        bankName = mTvSelectBank.getText().toString().trim();


        if (bankName.equals("请选择银行")){
            showToastMessage("请选择银行");
            return false;
        }


        if (Util.isEmpty(cardNum)){
            showToastMessage("请输入银行卡号");
            return false;
        }


        return true;
    }
}
